package com.ruoyi.lession.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.lession.common.ResultInfo;
import com.ruoyi.lession.entily.Lesson;
import com.ruoyi.lession.mapper.LessonMapper;
import com.ruoyi.lession.service.LessonService;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsLessonServiceImpl implements LessonService {
    @Resource
    LessonMapper lessonMapper;

    @Resource
    RestHighLevelClient restHighLevelClient;

    @Override
    public List<Lesson> findByService() {
        QueryWrapper<Lesson> queryWrapper = new QueryWrapper<>();
        List<Lesson> lessons = lessonMapper.selectList(queryWrapper);
        return lessons;
    }

    @Override
    public ResultInfo findByEsLessonList(Lesson lesson) {

/**
 * ES 查询 和 复杂条件搜索  根据课程名称高亮   根据 时间区间查询
 * @param classSchedule
 * @return
 */
            SearchRequest searchRequest = new SearchRequest("eslesson");
            searchRequest.types("eslesson");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            // 高亮 字段
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("name");
            //前置标签
            highlightBuilder.preTags("<span style='color:red'>");
            //后置标签
            highlightBuilder.postTags("</span>");
            searchSourceBuilder.highlighter(highlightBuilder);

            //模糊查询、区间查询
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            if (!StringUtils.isEmpty(lesson.getName())){
                MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", lesson.getName());
                boolQueryBuilder.must(matchQueryBuilder);
            }
            //区间查询 (根据课程价格范围)

            searchSourceBuilder.query(boolQueryBuilder);
            //根据价格倒叙排序
            searchSourceBuilder.sort("creattime", SortOrder.DESC);
            searchRequest.source(searchSourceBuilder);

        SearchResponse search = null;
        try {
            search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //分页
        //获取 总页数
            long total = search.getHits().getTotalHits().value;

            SearchHit[] hits = search.getHits().getHits();
            ArrayList<Object> list = new ArrayList<>();
            for (SearchHit hit : hits) {
                //获取ID
                String id = hit.getId();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                //插入ID
                sourceAsMap.put("id",id);
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                if (highlightFields.size()>0){
                    //替换高亮字段
                    HighlightField highlightField = highlightFields.get("name");
                    String name = highlightField.getFragments()[0].toString();
                    sourceAsMap.put("name",name);
                }
                list.add(sourceAsMap);
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("list",list);
            map.put("total",total);

            return new ResultInfo(true,1,"查询成功",map);

    }
}
